//Copyright (C) 2012 Chirila Alexandru
//This file is part of Mirror FTP
//
//Mirror FTP is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//Mirror FTP is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with Mirror FTP.  If not, see <http://www.gnu.org/licenses/>.

#include "filenode.h"

file_node::file_node() {
	name="";
	type='f';
	path="";
	mdtm="";
	size=0;
	check=false;
}

file_node::~file_node() {
	file_node *temp;
	list<file_node*>::iterator it;
	for (it=children.begin();it!=children.end();++it){
		temp=*it;
		delete temp;
	}
}
void file_node::add_child(char t,string n ,string p,long long s,string n_mdtm){
	///Adds child to current file structure node.
	///\param t 'f' - file \n 'd' - directory \n 'm' - mirror
	///\param n name
	///\param p path
	///\param s size
	///\param n_mdtm last modification date timestamp

	file_node *temp=new file_node;
	temp->set_type(t);
	temp->set_name(n);
	temp->set_size(s);
	temp->set_path(p);
	temp->set_mdtm(n_mdtm);
	children.push_back(temp);
}

int file_node::check_in(string p){
	///Checks whether the given path name si allready in the nodes children list (except the last one)
	///\param p path name
	///\return 1 if it allready exists
	///\return 0 if not

	file_node *node;
	list<file_node*>::iterator it;
	for (it=children.begin();it!=(--children.end());++it){
		node=*it;
		if (node->get_path()==p)
			return 1;
	}
	return 0;
}

void file_node::print_node(){
	///debug

	cout << path << " - " << check <<endl;
}

void file_node::print_struct(){
	///debug

	queue<file_node*> Q;
	file_node *node;
	list<file_node*>::iterator it;

	Q.push(this);
	while(!Q.empty()){
		node=Q.front();
		Q.pop();
		node->print_node();
		for (it=node->children.begin();it!=node->children.end();++it){
			Q.push(*it);
		}

	}
}

file_node* file_node::find_child_by_name(string n){
	list<file_node*>::iterator it;
	file_node *temp;
	for (it=children.begin();it!=children.end();++it){
		temp=*it;
		if (n==temp->get_name())
			return temp;
	}
	return NULL;
}

void file_node::dump_to_xml(fstream *file){
	///dumps whole tree structure to an xml format
	///starting from this

	stringstream ss,sst;
	string s,st,sc;
	ss << size;
	ss >> s;
	sst << type;
	sst >> st;
	string x;
	sc=(check==true)?"1":"0";

	file->write(x.c_str(),x.length());

	if (type=='d' || type=='m'){
		x="<"+st+" name=\""+name+"\" path=\""+path+"\" check=\"" + sc + "\" >\n";
		file->write(x.c_str(),x.length());

		list<file_node*>::iterator it;
		file_node* temp;
		for (it=children.begin();it!=children.end();++it){
			temp=*it;
			temp->dump_to_xml(file);
		}
		x="</"+st+">\n";
		file->write(x.c_str(),x.length());
	}
	else if (type=='f'){
		x="<"+st+" name=\""+name+"\" path=\""+path+"\" size=\""+
				s+"\" mdtm=\""+mdtm+"\" check=\"" +sc + "\" />\n";
		file->write(x.c_str(),x.length());
	}


}
